package com.nf28.removies.viewtimeline;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import com.nf28.removies.dto.Film;
import com.nf28.removies.viewtimeline.Scale.TimeScale;

public class FilmSorter {
	public static String defaultNoDate = "----";
	private static String[] nomMois = { "January", "February", "March", "April",
			"May", "June", "July", "August", "September", "October",
			"November", "December" };
	
	public static String getMoisFromString(String mm) {
		return nomMois[Integer.parseInt(mm)-1];
	}

	public static String getMois(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return nomMois[cal.get(Calendar.MONTH)];
	}

	private static String getStringScaleFilmSort(Date date, TimeScale scale) {
		switch (scale) {
		case YEAR:
			return new SimpleDateFormat("yyyy").format(date);
		case MONTH:
			return new SimpleDateFormat("MM").format(date);
		case DAY:
			return new SimpleDateFormat("dd").format(date);
		case HOUR:
			return new SimpleDateFormat("hh:mm:ss").format(date);
		default:
			return "---";
		}
	}

	public static HashMap<String, ArrayList<Film>> sortFilmsYear(
			ArrayList<Film> films) {
		HashMap<String, ArrayList<Film>> tmp = new HashMap<String, ArrayList<Film>>();
		for (Film film : films) {
			addFilmYear(tmp, film, TimeScale.YEAR);
		}
		return tmp;
	}

	public static HashMap<Integer, HashMap<String, ArrayList<Film>>> sortFilmsMount(
			ArrayList<Film> films) {
		HashMap<Integer, HashMap<String, ArrayList<Film>>> tmp = new HashMap<Integer, HashMap<String, ArrayList<Film>>>();
		for (Film film : films) {
			addFilmMount(tmp, film, TimeScale.MONTH);
		}
		return tmp;
	}

	public static HashMap<Integer, HashMap<Integer, HashMap<String, ArrayList<Film>>>> sortFilmsDay(
			ArrayList<Film> films) {
		HashMap<Integer, HashMap<Integer, HashMap<String, ArrayList<Film>>>> tmp = new HashMap<Integer, HashMap<Integer, HashMap<String, ArrayList<Film>>>>();
		for (Film film : films) {
			addFilmDay(tmp, film, TimeScale.DAY);
		}
		return tmp;
	}

	public static HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<String, ArrayList<Film>>>>> sortFilmsHour(
			ArrayList<Film> films) {
		HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<String, ArrayList<Film>>>>> tmp = new HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<String, ArrayList<Film>>>>>();
		for (Film film : films) {
			addFilmHour(tmp, film, TimeScale.HOUR);
		}
		return tmp;
	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	private static void addFilmYear(HashMap<String, ArrayList<Film>> tmp,
			Film film, TimeScale scale) {
		if (film.getViewdate() == null) {
			if (tmp.get(defaultNoDate) == null) {
				tmp.put(defaultNoDate, new ArrayList<Film>());
			}
			tmp.get(defaultNoDate).add(film);
		} else {
			String folder = getStringScaleFilmSort(film.getViewdate(), scale);
			if (tmp.get(folder) == null) {
				tmp.put(folder, new ArrayList<Film>());
			}
			tmp.get(folder).add(film);
		}
	}

	private static void addFilmMount(
			HashMap<Integer, HashMap<String, ArrayList<Film>>> tmp, Film film,
			TimeScale scale) {
		Date filmDate = film.getViewdate();
		if (filmDate == null)
			return;
		int unit = filmDate.getYear();
		if (tmp.get(unit) == null) {
			tmp.put(unit, new HashMap<String, ArrayList<Film>>());
		}
		addFilmYear(tmp.get(unit), film, scale);
	}

	private static void addFilmDay(
			HashMap<Integer, HashMap<Integer, HashMap<String, ArrayList<Film>>>> tmp,
			Film film, TimeScale scale) {
		Date filmDate = film.getViewdate();
		if (filmDate == null)
			return;
		int unit = filmDate.getMonth();
		if (tmp.get(unit) == null) {
			tmp.put(unit,
					new HashMap<Integer, HashMap<String, ArrayList<Film>>>());
		}
		addFilmMount(tmp.get(unit), film, scale);
	}

	private static void addFilmHour(
			HashMap<Integer, HashMap<Integer, HashMap<Integer, HashMap<String, ArrayList<Film>>>>> tmp,
			Film film, TimeScale scale) {
		Date filmDate = film.getViewdate();
		if (filmDate == null)
			return;
		int unit = filmDate.getDay();
		if (tmp.get(unit) == null) {
			tmp.put(unit,
					new HashMap<Integer, HashMap<Integer, HashMap<String, ArrayList<Film>>>>());
		}
		addFilmDay(tmp.get(unit), film, scale);
	}

}
